package com.itrus.contract.extension.shardinsphere.algorithm;

import com.itrus.contract.extension.shardinsphere.enumeration.TableRulesEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

@Slf4j
public class NormalShardingAlgorithm implements StandardShardingAlgorithm<Integer> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Integer> shardingValue) {
        String targetTable = shardingValue.getLogicTableName();
        if (availableTargetNames.contains(targetTable)){
            return targetTable;
        }
        throw new UnsupportedOperationException("月分片无效的表名称："+targetTable);
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Integer> shardingValue) {
        Collection<String> list = Arrays.asList(shardingValue.getLogicTableName());
        return availableTargetNames;
    }

    @Override
    public String getType() {
        return String.valueOf(TableRulesEnum.NORMAL.getIndex());
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}
